package com.moonciki.artadmin.core.utils;

import com.moonciki.artadmin.core.enums.ResponseEnum;
import com.moonciki.artadmin.core.exception.CustomException;

/**
 * @author 岳仕奇
 * @description: TODO
 * @date 2021/11/17 10:39
 */
public class NumberUtil {

    /**
     * 判断大小
     * 大于0：第一个大
     * 等于0：相等
     * 小于0：第二个大
     * @param one
     * @param two
     * @return
     */
    public static long compareValue(long one, long two) {
        return one - two;
    }

    public static long compareValue(Object obj1, long df1, Object obj2, long df2) {
        long one = getNumberValue(obj1, df1);
        long two = getNumberValue(obj2, df2);

        return compareValue(one, two);
    }

    public static long compareValue(Object obj1, long df1, long two) {
        long one = getNumberValue(obj1, df1);

        return compareValue(one, two);
    }

    /**
     * 考虑到各种类型范围，所以统一转成long 做比较
     * @param obj
     * @param defaultValue
     * @return
     */
    public static long getNumberValue(Object obj, long defaultValue){
        long result = defaultValue;
        if(obj != null){
            if(obj instanceof Long){
                result = (Long)obj;
            }else if(obj instanceof Integer){
                result = ((Integer)obj).longValue();
            }else if(obj instanceof Short){
                result = ((Short)obj).longValue();
            }else if(obj instanceof Byte){
                result = ((Byte)obj).longValue();
            }else{
                throw CustomException.createException(ResponseEnum.request_error.info("does not support yet!"));
            }
        }

        return result;
    }

}
